.PHONY: all debug utils clean windows obj
LDFLAGS=-lSDL2 -lSDL2_net -lSDL2_ttf
CC=cc
CFLAGS=-O1 -Wall
ifeq ($(CC), tcc)
CFLAGS+=-DSDL_DISABLE_IMMINTRIN_H
endif
TARGET=./build/life
MAIN_SRC=$(wildcard src/*.c)

ALL_OBJ=$(subst src/,build/,$(MAIN_SRC:.c=.o))

UTILS_SRC=$(wildcard utils/*.c)
UTILS=$(subst utils/,build/,$(UTILS_SRC:.c=))

LIB_OBJ:=$(filter-out build/main.o,$(ALL_OBJ))
LIB_OBJ:=$(filter-out build/graphic.o,$(LIB_OBJ))

build/%.o: src/%.c
	$(CC) -c $(CFLAGS) -o $@ $^

build/%: utils/%.c
	$(CC) $(LIB_OBJ) $(LDFLAGS) $(CFLAGS) -o $@ $^

all: obj
	$(MAKE) utils CFLAGS="$(CFLAGS)" CC="$(CC)" LDFLAGS="$(LDFLAGS)"
	$(CC) $(ALL_OBJ) $(CFLAGS) $(LDFLAGS) -o $(TARGET)
	cp ./media/terminus.ttf ./build


obj: $(ALL_OBJ)

utils: $(UTILS)

debug: CFLAGS += -g
debug: all

clean:
	rm build/*

windows: CC=x86_64-w64-mingw32-gcc
windows: UTILS=$(subst utils/,build/,$(UTILS_SRC:.c=.exe))
windows: TARGET := $(TARGET).exe
windows: CFLAGS += -DSDL_main_h_
windows: all

emscripten: all
	./build/mkworld 64 64 webworld.bin
	$(MAKE) clean
	$(MAKE) obj CC=emcc CFLAGS="$(CFLAGS) -sUSE_SDL=2" LDFLAGS=""
	emcc $(ALL_OBJ) $(CFLAGS) --preload-file webworld.bin -sUSE_SDL=2 -sLEGACY_GL_EMULATION -o $(TARGET).html

headless: CFLAGS += -DNO_GRAPHIC
headless: LDFLAGS =
headless: all
